package com.teatime.client;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.database.Cursor;

public class TeaTimeUtils {
	static public View getTopView(Context context) {
		return ((Activity)context).getWindow().getDecorView();
	}
	
	static public Rect getWindowVisibleRect(Context context) {
		View view = getTopView(context);
		if (view != null) {
			Rect rect = new Rect();
			view.getWindowVisibleDisplayFrame(rect);
			return rect;
		}
		return null;
	}
	
	static public int getWindowVisibleWidth(Context context) {
		Rect rect = getWindowVisibleRect(context);
		if (rect != null) {
			return rect.width();
		}
		return 0;
	}
	
	static public int getWindowVisibleHeight(Context context) {
		Rect rect = getWindowVisibleRect(context);
		if (rect != null) {
			return rect.height();
		}
		return 0;
	}
	
	static public int getScreenWidth(Context context) {
		return ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
	}
	
	static public int getScreenHeight(Context context) {
		return ((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
	}
	
	static public int getWindowStatusBarHeight(Context context) {
		Rect rect = getWindowVisibleRect(context);
		if (rect != null) {
			return rect.top;
		}
		return 0;
	}
	
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	
	static public Uri getFileUri(Context context, 
			String fileName /*just file name or a relative path */) {
		File path = context.getFileStreamPath(fileName);
        /*path.delete();
        tempStream = context.openFileOutput(fileName, 0);
        tempStream.write(data);
        tempStream.close();*/
        return Uri.fromFile(path);
	}
	
	static public String Uri2Path(Context context, Uri uri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor actualimagecursor = ((Activity)context).managedQuery(uri, proj, null, null, null);
		int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		actualimagecursor.moveToFirst();
		return actualimagecursor.getString(actual_image_column_index);
	}
	
	static public boolean mobileValidate(String mobile) {
		if (mobile == null) {
			return false;
		}
		if (mobile.length() != 11) {
			return false;
		}
		if (!mobile.startsWith("1")) {
			return false;
		}
		return true;
	}
	
}
